package com.renjianbo.project.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.renjianbo.project.common.model.PageInfo;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * json转换类
 */
@Slf4j
public class JsonUtil {

    public static final String serializeAllExcept = "serializeAllExcept";
    public static final String filterOutAllExcept = "filterOutAllExcept";

    /**
     * 对象转json字符串
     */
    public static <T> String objectToJson(T T){
        String json="";
        try {
            ObjectMapper jsonMapper = new ObjectMapper();
            jsonMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            json = jsonMapper.writeValueAsString(T);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 将json字符串转化为实体POJO
     */
    public static <T> T JSONToObj(String jsonStr, Class<T> T) {
        T t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            t = objectMapper.readValue(jsonStr, T);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * list泛型转型
     */
    public static <T> List<T> listTransform(List oldList, Class<T> beanType){
        List<T> list = new ArrayList<>();
        for(Object object: oldList){
            //  Feature.OrderedField：保持顺序
            T t = JSONObject.parseObject(JSON.toJSONString(object), beanType, Feature.OrderedField);
            list.add(t);
        }

        return list;
    }

    /**
     * 分页数据类型转型
     */
    public static <T> PageInfo<T> iPageTransform(IPage iPage, Class<T> beanType) {
        //  转化分页数据
        List<T> list = new ArrayList<>();
        for(Object object: iPage.getRecords()){
            //  Feature.OrderedField：保持顺序
            T t = JSONObject.parseObject(JSON.toJSONString(object), beanType, Feature.OrderedField);
            list.add(t);
        }

        //  新分页数据
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setCurrent((int) iPage.getCurrent());
        pageInfo.setPages((int) iPage.getPages());
        pageInfo.setSize((int) iPage.getSize());
        pageInfo.setTotal((int) iPage.getTotal());
        pageInfo.setRecords(list);

        return pageInfo;
    }

    /**
     * 分页数据类型转型
     */
    public static <T> PageInfo<T> pageTransform(IPage<T> iPage) {
        //  新分页数据
        PageInfo<T> newPageInfo = new PageInfo<>();
        newPageInfo.setCurrent((int) iPage.getCurrent());
        newPageInfo.setPages((int) iPage.getPages());
        newPageInfo.setSize((int) iPage.getSize());
        newPageInfo.setTotal((int) iPage.getTotal());
        newPageInfo.setRecords(iPage.getRecords());

        return newPageInfo;
    }

    /**
     * 分页数据类型转型
     */
    public static <T> PageInfo<T> pageInfoTransform(PageInfo pageInfo, Class<T> beanType) {
        //  转化分页数据
        List<T> list = new ArrayList<>();
        for(Object object: pageInfo.getRecords()){
            //  Feature.OrderedField：保持顺序
            T t = JSONObject.parseObject(JSON.toJSONString(object), beanType, Feature.OrderedField);
            list.add(t);
        }

        //  新分页数据
        PageInfo<T> newPageInfo = new PageInfo<>();
        newPageInfo.setCurrent(pageInfo.getCurrent());
        newPageInfo.setPages(pageInfo.getPages());
        newPageInfo.setSize(pageInfo.getSize());
        newPageInfo.setTotal(pageInfo.getTotal());
        newPageInfo.setRecords(list);

        return newPageInfo;
    }

    /**
     * 返回json 节点
     */
    public static JsonNode getJsonNode(String jsonStr) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonNode;
    }

    /**
     * 返回json的value
     */
    public static String getJsonValue(JsonNode node, String key) {
        JsonNode result = node.get(key);
        if(result!=null){
            if(result.getNodeType().name().equalsIgnoreCase("STRING")){
                return result.asText();
            }else {
                return result.toString();
            }
        }
        return null;
    }

    public static void  main(String[] args){
        log.info("{}", Math.pow( 10, 2));



//                String s = "{\"addresslist-explorer\":{\"result\":{},\"query\":{\"display-name\":\"3333\"}}}";
//        JsonNode node = JsonUtil.getJsonNode(s);
//        JsonNode node1 = JsonUtil.getJsonNode(JsonUtil.getJsonValue(node,"addresslist-explorer"));
//        System.out.println(JsonUtil.getJsonValue(node,"addresslist-explorer"));
//        System.out.println(JsonUtil.getJsonValue(node1,"result"));
    }

    /**
     * 将请求参数转换为json格式的string
     * map格式的转化成json格式
     */
//    public static String getRequestMapToJson(Map<String, Object> map) {
//
//        return new Gson().toJson(map);
//    }

}
